--[[
任务通关结算逻辑
getReward				-- 领取通关结算奖励
setReward				-- 设置通关结算奖励数据
sendCopyRewardNotify	-- 下发副本通关奖励通知

内存里面copyReward 保存的数据
copyID			-- 副本id
cAims			-- 完成的挑战目标的id
exp				-- 副本奖励经验
worldExpAdd	    -- 副本奖励经验世界加成
teamAdd			-- 组队经验加成
familyAdd		-- 军团经验加成
vipAdd			-- 仙尊经验加成
yinliang		-- 副本奖励铜币
item			-- 副本奖励道具数组
--]]
local Lang = require("common.Lang")
local Util = require("common.Util")
local RandomLogic = require("common.RandomLogic")
local ExcelMap = require("excel.Map")
local CopyConfig = ExcelMap.copy
local ExcelCopyAimConf = require("excel.CopyAim")
local Msg = require("core.Msg")
local Map = require("core.Map")
local ObjHuman = require("core.ObjHuman")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local Copy = require("copy.CopyL")
local Aim = require("copy.Aim")
local CopyDefine = require("copy.Define")
local CopyCallback = require("copy.CallbackL")

local RewardItemGridCache = {}		-- 任务奖励道具grid缓存 避免频繁new grid

function getReward(human, copyID)
	if human.copyReward == nil then return end
	if human.copyReward.copyID ~= copyID then return end
	
	local expAdd = human.copyReward.exp
	local expAddWorld = human.copyReward.worldExpAdd

	local yinliangAdd = human.copyReward.yinliang
	
	local rewardItems = human.copyReward.item
	
	human.copyReward = nil
	
	-- 加经验 
	ObjHuman.addExp(human, expAdd, expAddWorld)
	
	-- 加钱
	ObjHuman.addBindYinliang(human, yinliangAdd, "copy_end_reward")
	
	-- 发道具
	if rewardItems and rewardItems[1] then
		local itemGrids = rewardItems
		ActivityUtil.sendActReward(human.db._id, itemGrids, {Map.scene_id_to_map_id[human.scene_id]}, Lang.COPY_END_REWARD_TITLE, Lang.COPY_END_REWARD_CONTENT, "copy_end_reward")
	end
end

function setReward(copyID, roomID)
	local rewardTable = CopyCallback.onGetFinishReward(copyID, roomID)

  if 1 then return end
	if rewardTable == nil and ExcelCopyAimConf.reward[copyID] == nil then return end -- 副本没有配置对应的通关结算奖励
	
	local cAims = {}	-- 副本挑战目标达成度
	local cAimFinishCount = 0
	
	local aimRecord = Aim.aimRoomStatus[roomID]
	for tempID, tempConfig in pairs(ExcelCopyAimConf[copyID]) do
		if tempConfig.mainLoop == CopyDefine.AIM_TYPE_CHALLENGE then
			cAims[tempID] = 0
			if aimRecord[tempID] and aimRecord[tempID].status == CopyDefine.AIM_STATUS_FINISHED then
				cAims[tempID] = 1
				cAimFinishCount = cAimFinishCount + 1
			end
		end
	end
	
	local rewardLV = cAimFinishCount + 1
	
	local expAdd = 0
	local yinliangAdd = 0
	local itemAdd = nil
	if rewardTable then
		expAdd = rewardTable.exp or 0
		yinliangAdd = rewardTable.yinliang or 0
		if rewardTable.grids then
			itemAdd = {}
			Util.copyTable(rewardTable.grids, itemAdd)
		end
	else
		expAdd = ExcelCopyAimConf.reward[copyID].exp
		local param = ExcelCopyAimConf.reward[copyID].expParam[rewardLV]-- 副本奖励经验系数	
		expAdd = math.ceil(expAdd * param)	
		
		yinliangAdd = ExcelCopyAimConf.reward[copyID].money[rewardLV]	-- 总金钱奖励
		
		local item = getCopyRewardItemGrid(copyID, rewardLV)
		itemAdd = {}
		itemAdd[1] = item
	end
	
	local humanList = Copy.getCopyHumanList(copyID, roomID)
	for i = 1, #humanList do
		local human = humanList[i]
		local teamAdd = 0
		local familyAdd = 0
		local vipAdd = 0		local worldExpAdd = 0
		
		human.copyReward = {}
		human.copyReward.copyID = copyID			-- 副本id
		human.copyReward.cAims = cAims				-- 完成的挑战目标的id
		human.copyReward.exp = expAddHuman			-- 副本奖励经验	
		human.copyReward.worldExpAdd = worldExpAdd	-- 副本奖励经验世界加成
		human.copyReward.teamAdd = teamAdd			-- 队伍经验加成
		human.copyReward.familyAdd = familyAdd		-- 军团经验加成
		human.copyReward.vipAdd = vipAdd			-- 仙尊经验加成		
		human.copyReward.yinliang = yinliangAdd	-- 副本奖励铜币		
		human.copyReward.item = itemAdd		
	end
end

function sendCopyRewardNotify(human, copyID)
	if human.copyReward == nil then return end
	if human.copyReward.copyID ~= copyID then return end

	local copyConfig = CopyConfig[human.copyReward.copyID]
	
	local msgRet = Msg.gc.GC_COPY_REWARD_NOTIFY
	msgRet.copyID = human.copyReward.copyID
	msgRet.copyName = copyConfig.name
	msgRet.copyType = copyConfig.type

	Msg.send(msgRet, human.fd)
	
	getReward(human, copyID)
	
	return true
end

function getCopyRewardItemGrid(copyID, rewardIndex)
	local itemPackID = ExcelCopyAimConf.reward[copyID].itemParam[rewardIndex]
	local config = ExcelCopyAimConf.rewardItem[itemPackID]
	if config == nil then return end
	
	local bind = true
	local index = RandomLogic.getRandom1(config.item, nil, 2)
	if config.item[index][5] and config.item[index][5] == 0 then bind = nil end
	local itemID = config.item[index][1]
	local itemCount = config.item[index][3]
	local itemQuality = config.item[index][4]
	
	RewardItemGridCache[copyID] = RewardItemGridCache[copyID] or {}
	if RewardItemGridCache[copyID][itemID] == nil then
		if not itemQuality then
			RewardItemGridCache[copyID][itemID] = Grid.create(itemID, itemCount, bind)
		else
			RewardItemGridCache[copyID][itemID] = Grid.create(itemID, itemCount, bind, ItemDefine.getQualityOut(itemQuality))
		end
	end
	
	return RewardItemGridCache[copyID][itemID]
end
